<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
    * Class adminUsers
    ****
    * This class represents the users part of a solution
    ****
    * @package adminModules
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    ***/
    class adminShopreports extends adminModule{

        /**
         * Process user inputs
         *
         * @return void
         **/
        public function process(){
            //do we want a receipt?
            if($_GET['pdf']){
                $this->getReceipt($_GET['oid']);
                exit;
            }


            $this->smarty->assign('vats',$this->db->getAssoc("SELECT DISTINCT tax, tax*100||' %' AS taxp FROM node_shop_article ORDER BY tax"));
            $this->smarty->assign('suppliers',$this->db->getAssoc("SELECT id, name FROM shop_suppliers ORDER BY name"));
            $this->smarty->assign('articles',$this->db->getAssoc("SELECT id, ticket_title AS teaser_title FROM node_shop_article WHERE teaser_title != '' AND teaser_title IS NOT NULL ORDER BY teaser_title"));

            if(!$_GET['from_Year']){
                $this->smarty->assign('from','-1 Week');
            }else{
                $this->smarty->assign('from',$_GET['from_Year'].'-'.$_GET['from_Month'].'-'.$_GET['from_Day']);
            }

            if($_GET['until_Year']){
                $this->smarty->assign('until',$_GET['until_Year'].'-'.$_GET['until_Month'].'-'.$_GET['until_Day']);
            }

            ## Actual Report ##
            #
            //$sql = "SELECT node_shop_order.* FROM node_shop_order ";
            $clause = array();

            if($_GET['from_Year']){
                $clause[] = " node_shop_order.created >= '".$_GET['from_Year'].'-'.$_GET['from_Month'].'-'.$_GET['from_Day']." 00:00:00' ";
            }else{
                $clause[] = " node_shop_order.created >= '".date('Y-m-d',strtotime('-1 Week'))." 00:00:00' ";
            }

            if($_GET['until_Year']){
                $clause[] = " node_shop_order.created <= '".$_GET['until_Year'].'-'.$_GET['until_Month'].'-'.$_GET['until_Day']." 23:59:59' ";
            }

            if($_GET['supplier']){
                $clause[] = " node_shop_article.supplier = ".(int)$_GET['supplier']." ";
            }

            if($_GET['article']){
                $clause[] = " node_shop_article.id = ".(int)$_GET['article']." ";
            }

            if($_GET['order']){
                $clause[] = " node_shop_order.id = ".(int)$_GET['order']." ";
            }

            if($_GET['paid']){
                $clause[] = " node_shop_order.payed = ".(($_GET['paid']=='yes')?'TRUE':'FALSE')." ";
            }

            if($_GET['tax']){
                $clause[] = " node_shop_article.tax = ".(float)$_GET['tax']." ";
            }


            $this->smarty->assign('sum',$this->db->getOne("SELECT
                                                                    sum(node_shop_order_article.price*node_shop_order_article.quant1+node_shop_order_article.price_kids*node_shop_order_article.quant2)
                                                                FROM node_shop_order
                                                                LEFT JOIN node_shop_order_article ON node_shop_order.id = node_shop_order_article.order_id
                                                                LEFT JOIN node_shop_article ON node_shop_order_article.article_id = node_shop_article.id
                                                                WHERE ".implode(' AND ',$clause)));

            $data = $this->db->getAll("SELECT
                                                                    node_shop_order.*,
                                                                    node_shop_article.ticket_title AS articlename,
                                                                    node_shop_order_article.quant1,
                                                                    node_shop_order_article.quant2,
                                                                    node_shop_order_article.price AS price_adults,
                                                                    node_shop_order_article.price_kids
                                                                FROM node_shop_order
                                                                LEFT JOIN node_shop_order_article ON node_shop_order.id = node_shop_order_article.order_id
                                                                LEFT JOIN node_shop_article ON node_shop_order_article.article_id = node_shop_article.id
                                                                WHERE ".implode(' AND ',$clause)." ORDER BY node_shop_order.created DESC, node_shop_article.ticket_title");

            if(!$_GET['xls']){
                $this->smarty->assign('orders',$data);
            }else{
                /**
                    Excel Export
                 **/
                include_once(INA_ROOT."lib/excel/Worksheet.php");
                include_once(INA_ROOT."lib/excel/Workbook.php");

                header("Content-type: application/vnd.ms-excel");
                header("Content-Disposition: attachment; filename=orders.xls" );
                header("Expires: 0");
                header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
                header("Pragma: public");

                // Creating a workbook
                $workbook = new Workbook("-");

                // General formats
                $norm       = &$workbook->add_format();
                $norm->set_size(12);
                $norm->set_align('top');

                $norm_big   = &$workbook->add_format();
                $norm_big->set_size(14);

                $norm_wrap  = &$workbook->add_format();
                $norm_wrap->set_size(12);
                $norm_wrap->set_align('top');
                $norm_wrap->set_text_wrap();

                $bold       = &$workbook->add_format();
                $bold->set_size(12);
                $bold->set_bold();
                $bold->set_align('top');

                $bold_big   = &$workbook->add_format();
                $bold_big->set_size(14);
                $bold_big->set_bold();

                $sum        = &$workbook->add_format();
                $sum->set_num_format('0.00');

                // Creating worksheet
                $worksheet = &$workbook->add_worksheet('Orders');

                //Writing Data
                $worksheet->write_string(0,0,'Order ID',$bold);
                $worksheet->write_string(0,1,'Created',$bold);
                $worksheet->write_string(0,2,'Created By',$bold);
                $worksheet->write_string(0,3,'Paid',$bold);
                $worksheet->write_string(0,4,'Voucher Sent',$bold);
                $worksheet->write_string(0,5,'Valid From',$bold);
                $worksheet->write_string(0,6,'Item Name',$bold);
                $worksheet->write_string(0,7,'Adults',$bold);
                $worksheet->write_string(0,8,'Adults Price',$bold);
                $worksheet->write_string(0,9,'Children',$bold);
                $worksheet->write_string(0,10,'Children Price',$bold);
                $worksheet->write_string(0,11,'Item Sum',$bold);
                $worksheet->write_string(0,12,'VAT',$bold);
                $worksheet->write_string(0,13,'Order Total',$bold);


                $worksheet->set_column(1,12,20);

                $line   = 1;
                $forder = 0;
                foreach($data as $row){
                    //if($row['id'] != $forder){
                        $worksheet->write_number($line,0,$row['id'],$norm);
                        $worksheet->write_string($line,1,date('Y-m-d H:i:s',strtotime($row['created'])),$norm);
                        $worksheet->write_string($line,2,$row['name'].' <'.$row['mail'].'>',$norm);
                        $worksheet->write_string($line,3,$row['payed'],$norm);
                        $worksheet->write_string($line,4,$row['notified'],$norm);
                        $worksheet->write_string($line,5,date('Y-m-d',strtotime($row['term'])),$norm);

                        $worksheet->write_string($line,6,$row['articlename'],$norm);
                        $worksheet->write_number($line,7,$row['quant1'],$norm);
                        $worksheet->write_number($line,8,$row['price_adults'],$sum);
                        $worksheet->write_number($line,9,$row['quant2'],$norm);
                        $worksheet->write_number($line,10,$row['price_kids'],$sum);

                        $worksheet->write_number($line,11,round($row['quant1']*$row['price_adults']+$row['quant2']*$row['price_kids'],2),$sum);
                        $worksheet->write_number($line,12,10,$norm);
                        $worksheet->write_number($line,13,$row['price'],$sum);
                        $line++;
                        //$forder = $row['id'];
                    //}
                }

                $workbook->close();
                exit;
            }

            #
            ## /Report
        }


        protected function getReceipt($oid){
            header("Content-type: application/pdf.acrobat");
            header("Content-Disposition: attachment; filename=rechnung".$oid.".pdf" );
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
            header("Pragma: public");


            //get the data
            $order = $this->db->getRow("SELECT
                                            id,
                                            name,
                                            price,
                                            created,
                                            date_part('year', created) AS c_year
                                        FROM node_shop_order WHERE id = ".(int)$oid);

            $articles = $this->db->getAll("SELECT
                                                node_shop_order_article.article_id,
                                                ticket_title AS name,
                                                node_shop_order_article.quant1 AS adults,
                                                node_shop_order_article.quant2 AS kids,
                                                node_shop_order_article.quant1*node_shop_order_article.price + node_shop_order_article.quant2*node_shop_order_article.price_kids AS price
                                            FROM node_shop_order_article
                                            LEFT JOIN node_shop_article ON node_shop_order_article.article_id = node_shop_article.id
                                            WHERE node_shop_order_article.order_id = ".(int)$oid);

            include (INA_ROOT.'lib/pdf/class.ezpdf.php');
            $this->smarty->loadLang('voucher.txt');
            $lang = $this->smarty->lang;

            $pdf =& new Cezpdf();
            $pdf->selectFont(INA_ROOT.'lib/pdf/fonts/Helvetica.afm');
            $pdf->ezSetCmMargins(1,1,2,2);
            $pdf->ezImage(ROOT.'templates/vh.jpg',0,500,'none','left');
            $pdf->ezText('<b>'.$lang['rnum'].'. '.$order['id'].'-'.$order['c_year'].'</b>',20,array('justification' => 'centre'));
            $pdf->ezText('<b>'.$lang['zu'].'</b>',16,array('justification' => 'centre'));
            $pdf->ezText('<b>'.$lang['order'].'</b>',20,array('justification' => 'centre'));
            $pdf->ezText('Nummer: '.$order['id'],16,array('justification' => 'centre'));

            $pdf->ezText('  ',16,array('justification' => 'centre'));
            $pdf->ezText($lang['aus'].': '.$order['name'],14,array('justification' => 'centre'));
            $pdf->ezText('  ',16,array('justification' => 'centre'));
            $pdf->ezText('  ',16,array('justification' => 'centre'));
            $pdf->ezImage(ROOT.'templates/line.jpg',5,480,'none','left');

            $data = array();

            foreach($articles as $article){
                $data[] = array('service'=>$article['name'],   'adults'=>$article['adults'], 'kids'=>$article['kids'], 'price'=>number_format($article['price'],2).' EUR');
            }

            $data[] = array('service'=>' ',               'adults'=>' ', 'kids'=>' ', 'price'=>' ');
            $data[] = array('service'=>$lang['t1'].':',    'adults'=>' ', 'kids'=>' ', 'price'=>number_format(round($order['price']/1.1,2),2).' EUR');
            $data[] = array('service'=>$lang['t2'].':',       'adults'=>' ', 'kids'=>' ', 'price'=>number_format(round($order['price']-($order['price']/1.1),2),2).' EUR');
            $data[] = array('service'=>'<b>'.$lang['t3'].':</b>',   'adults'=>' ', 'kids'=>' ', 'price'=>'<b>'.number_format($order['price'],2).' EUR</b>');


            $pdf->ezTable($data,array('service'=>'Service','adults'=>$lang['erw'],'kids'=>$lang['kids'],'price'=>$lang['price']),$lang['voucher'],array('shaded'=>0));

            $pdf->ezText('  ',16,array('justification' => 'centre'));
            $pdf->ezText('  ',16,array('justification' => 'centre'));
            $pdf->ezImage(ROOT.'templates/line.jpg',5,480,'none','left');

            $pdf->ezText('Viennahighlights.com operated by:',12,                           array('justification' => 'centre'));
            $pdf->ezText('TO BE DEFINED Ges.m.b.H. | Karthaeuserstrasse 8, A-1190 Wien',12,array('justification' => 'centre'));
            $pdf->ezText('Fb Nummer: XXXXXXX | Handelsgericht Wien UID: ATUXXXXX',12,      array('justification' => 'centre'));
            $pdf->ezText('Kto Nummer 231 993 BLZ 20272 - Sparkasse Waldviertel Mitte',12,  array('justification' => 'centre'));

            //$pdf->ezStream();
            echo $pdf->ezOutput();
        }


        /**
         * Set action template to controlPanel.html
         *
         * @return void
         **/
        protected function setTemplate(){
            $this->setActionTemplate('shopreports.html');
        }
    }
?>